using System;
using System.Windows.Input;
using CommunityToolkit.Mvvm.Input;

namespace CANBusTools.ViewModels
{
    public class WarningControlViewModel : ViewModelBase
    {
        private readonly object _busHelper;

        public string[] LrSonarIds { get; } = { "1.FL", "2.LM", "3.RL", "4.FR", "5.RM", "6.RR" };
        public string[] LrSonarValues { get; } = { "Invalid", "None", "zone1", "zone2", "Error" };

        public string[] FrSonarIds { get; } = { "1.FLC", "2.FLM", "3.FRM", "4.FRC", "5.RLC", "6.RLM", "7.RRM", "8.RRC" };
        public string[] FrSonarValues { get; } = { "Invalid", "None", "zone1", "zone2", "zone3", "zone4", "Error" };

        private string _selectedLrSonarId = string.Empty;
        public string SelectedLrSonarId
        {
            get => _selectedLrSonarId;
            set
            {
                if (_selectedLrSonarId != value)
                {
                    _selectedLrSonarId = value;
                    OnPropertyChanged(nameof(SelectedLrSonarId));
                }
            }
        }

        private string _selectedLrSonarValue = string.Empty;
        public string SelectedLrSonarValue
        {
            get => _selectedLrSonarValue;
            set
            {
                if (_selectedLrSonarValue != value)
                {
                    _selectedLrSonarValue = value;
                    OnPropertyChanged(nameof(SelectedLrSonarValue));
                }
            }
        }

        private string _selectedFrSonarId = string.Empty;
        public string SelectedFrSonarId
        {
            get => _selectedFrSonarId;
            set
            {
                if (_selectedFrSonarId != value)
                {
                    _selectedFrSonarId = value;
                    OnPropertyChanged(nameof(SelectedFrSonarId));
                }
            }
        }

        private string _selectedFrSonarValue = string.Empty;
        public string SelectedFrSonarValue
        {
            get => _selectedFrSonarValue;
            set
            {
                if (_selectedFrSonarValue != value)
                {
                    _selectedFrSonarValue = value;
                    OnPropertyChanged(nameof(SelectedFrSonarValue));
                }
            }
        }

        public ICommand SetLrWarningCommand { get; }
        public ICommand SetFrWarningCommand { get; }

        public WarningControlViewModel(object busHelper)
        {
            _busHelper = busHelper;

            SelectedLrSonarId = LrSonarIds[0];
            SelectedLrSonarValue = LrSonarValues[1];

            SelectedFrSonarId = FrSonarIds[0];
            SelectedFrSonarValue = FrSonarValues[1];

            SetLrWarningCommand = new RelayCommand(SetLrWarning);
            SetFrWarningCommand = new RelayCommand(SetFrWarning);
        }

        private void SetLrWarning()
        {
            try
            {
                int index = int.Parse(SelectedLrSonarId.Split('.')[0]);
                int value = Array.IndexOf(LrSonarValues, SelectedLrSonarValue);
                // TODO: 替换为实际调用 _busHelper.LRWarningSwitch(index, value);
                Console.WriteLine($"Set LR Warning: Index={index}, Value={value}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"设置 LR 声呐失败: {ex.Message}");
            }
        }

        private void SetFrWarning()
        {
            try
            {
                int index = int.Parse(SelectedFrSonarId.Split('.')[0]);
                int value = Array.IndexOf(FrSonarValues, SelectedFrSonarValue);
                // TODO: 替换为实际调用 _busHelper.FRWarningSwitch(index, value);
                Console.WriteLine($"Set FR Warning: Index={index}, Value={value}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"设置 FR 声呐失败: {ex.Message}");
            }
        }
    }
}
